﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace StandaloneSilverlightWoZ
{
    public partial class Page : UserControl
    {
        List<Point> _mousePointList;
        public List<Point> MousePointList
        {
            get
            {
                return _mousePointList;
            }
        }
        List<Line> _mouseTrailLinesAdded;
        bool _captureMouse = true;
        int _mouseMoveInvokeCount = 0;
        DateTime _startTime;
        TimeSpan _accumulatedTime;
        public TimeSpan AccumulatedTime
        {
            get
            {
                return _accumulatedTime;
            }
            set
            {
                _accumulatedTime = value;
            }
        }
        public Page()
        {
            InitializeComponent();
            _mousePointList = new List<Point>();
            _mouseTrailLinesAdded = new List<Line>();
            ResumeButton.IsEnabled = false;
            PauseButton.IsEnabled = false;
            EndButton.IsEnabled = false;
        }

        private void CurrentPageCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (_captureMouse)
            {
                _mouseMoveInvokeCount++;
                if (_mouseMoveInvokeCount == 5)
                {
                    _mousePointList.Add(e.GetPosition(CurrentPageCanvas));
                    _mouseMoveInvokeCount = 0;
                }
            }
        }

        private void ShowMouseTrailsButton_Clicked(object sender, RoutedEventArgs e)
        {
            if (_mouseTrailLinesAdded.Count == 0)
            {
                for (int i = 0; i < _mousePointList.Count - 1; i++)
                {
                    Line tempLine = new Line();
                    tempLine.Stroke = new SolidColorBrush(Colors.Blue);
                    tempLine.X1 = _mousePointList[i].X;
                    tempLine.Y1 = _mousePointList[i].Y;
                    tempLine.X2 = _mousePointList[i+1].X;
                    tempLine.Y2 = _mousePointList[i+1].Y;
                    CurrentPageCanvas.Children.Add(tempLine);
                    _mouseTrailLinesAdded.Add(tempLine);
                }
            }
            else
            {
                foreach (Line item in _mouseTrailLinesAdded)
                {
                    CurrentPageCanvas.Children.Remove(item);
                }
                _mouseTrailLinesAdded.Clear();
            }
        }

        private void StartButton_Clicked(object sender, RoutedEventArgs e)
        {
            _startTime = DateTime.Now;
            _accumulatedTime = new TimeSpan(0);
            StartButton.IsEnabled = false;
            PauseButton.IsEnabled = true;
            EndButton.IsEnabled = true;
            CommentsExpander.IsEnabled = true;
            CurrentPageCanvas.IsHitTestVisible = true;
        }

        public void PauseButton_Clicked(object sender, RoutedEventArgs e)
        {
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - _startTime.Ticks);
            _accumulatedTime = _accumulatedTime.Add(ts);
            TimeElapsedLabel.Text = _accumulatedTime.ToString();
            ResumeButton.IsEnabled = true;
            PauseButton.IsEnabled = false;
            CurrentPageCanvas.IsHitTestVisible = false;
        }

        public void ResumeButton_Clicked(object sender, RoutedEventArgs e)
        {
            _startTime = DateTime.Now;
            ResumeButton.IsEnabled = false;
            PauseButton.IsEnabled = true;
            CurrentPageCanvas.IsHitTestVisible = true;
        }

        private void EndButton_Clicked(object sender, RoutedEventArgs e)
        {
            PauseButton_Clicked(sender, e);
            LayoutRoot.IsHitTestVisible = false;
            Canvas overlayCanvas=new Canvas();
            overlayCanvas.Width = LayoutRoot.ActualWidth;
            overlayCanvas.Height = LayoutRoot.ActualHeight;
            overlayCanvas.Background = new SolidColorBrush(Color.FromArgb(116, 65, 65, 65));
            TextBlock uploadingDataTextBlock= new TextBlock();
            uploadingDataTextBlock.Text = "Please wait while the data is being uploaded. Do not close this page, you will automatically be redirected.";
            uploadingDataTextBlock.FontWeight = FontWeights.Bold;
            uploadingDataTextBlock.FontSize = 13;
            uploadingDataTextBlock.Foreground = new SolidColorBrush(Colors.White);
            uploadingDataTextBlock.TextAlignment = TextAlignment.Center;
            uploadingDataTextBlock.VerticalAlignment = VerticalAlignment.Center;
            uploadingDataTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
            Canvas.SetLeft(uploadingDataTextBlock, (LayoutRoot.ActualWidth / 2) - (uploadingDataTextBlock.ActualWidth / 2));
            Canvas.SetTop(uploadingDataTextBlock, LayoutRoot.ActualHeight / 2);
            Animation.LoadingAnimation loadingAnimation = new Animation.LoadingAnimation();
            Canvas.SetTop(loadingAnimation, (LayoutRoot.ActualHeight / 2) + uploadingDataTextBlock.ActualHeight);
            Canvas.SetLeft(loadingAnimation, (LayoutRoot.ActualWidth / 2) - (loadingAnimation.Width / 2));
            overlayCanvas.Children.Add(loadingAnimation);
            overlayCanvas.Children.Add(uploadingDataTextBlock);
            LayoutRoot.Children.Add(overlayCanvas);
            SilverlightWoZApplication.UploadData();
            ResumeButton.IsEnabled = false;
            PauseButton.IsEnabled = false;
            CurrentPageCanvas.IsHitTestVisible = false;
            

        }

        private void CommentsExpander_Expanded(object sender, RoutedEventArgs e)
        {
            PauseButton_Clicked(sender, e);
        }

        private void CommentsExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            ResumeButton_Clicked(sender, e);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            SilverlightWoZApplication.SetupWizardOfOz(this, CurrentPageCanvas);
            CurrentPageCanvas.IsHitTestVisible = false;
        }
    }
}
